package gwtI18nEditor.tools;

import java.util.Iterator;

public abstract class IteratorFilter<E> implements Iterator<E> {
	
	private final Iterator<E> iterator;

	private E next = null;
	
	public IteratorFilter(Iterator<E> iterator) {
		this.iterator = iterator;
	}

	@Override
	public boolean hasNext() {
		if (next!=null) return true;
		while (iterator.hasNext()) {
			next = iterator.next();
			if (keep(next)) return true;
		}
		return false;
	}

	@Override
	public E next() {
		if (next==null) {
			next = iterator.next();
			while (!keep(next)) {
				next = iterator.next();
			}
		}
		E result = next;
		next = null;
		return result;
	}

	@Override
	public void remove() {
		iterator.remove();
	}
	
	protected abstract boolean keep(E item);
}
